using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using 凌依.工具库;

public class 柏林噪声
{
    public static float[,] 柏林噪声叠加(int 规格, List<噪声参数> 参数)
    {
        long 开始时间 = 时间.时间戳_毫秒级();
        float[,] vs = new float[规格, 规格];

        float maxHeight = float.MinValue;
        float minHeight = float.MaxValue;

        System.Random random = new System.Random();

        foreach (var item in 参数)
        {
            item.噪声偏移.x = random.Next(0, 100000);
            item.噪声偏移.y = random.Next(0, 100000);
        }


        for (int y = 0; y < 规格; y++)
        {

            for (int x = 0; x < 规格; x++)
            {
                float y1 = 0;
                foreach (var item in 参数)
                {
                    float fenquency = item.噪声缩放;
                    float 振幅 = item.高度;
                    for (int i = 0; i < item.叠加次数; i++)
                    {
                        y1 += Mathf.PerlinNoise((x + item.噪声偏移.x) * fenquency, (y + item.噪声偏移.y) * fenquency) * 振幅;
                        fenquency *= item.粗糙度;
                        振幅 *= item.保留振幅;//小于一
                    }
                }
                if (y1 > maxHeight)
                    maxHeight = y1;
                else if (y1 < minHeight)
                    minHeight = y1;
                vs[x, y] = y1;
            }
        }


        for (int y = 0; y < 规格; y++)
        {
            for (int x = 0; x < 规格; x++)
            {
                vs[x, y] = Mathf.InverseLerp(minHeight, maxHeight, vs[x, y]);
            }
        }
        Debug.Log(时间.时间戳_毫秒级() - 开始时间);
        return vs;
    }
    public static float 柏林噪声叠加(int x, int y, List<噪声参数> 参数)
    {
        float y1 = 0;
        foreach (var item in 参数)
        {
            float fenquency = item.噪声缩放;
            float 振幅 = item.高度;
            for (int i = 0; i < item.叠加次数; i++)
            {
                y1 += Mathf.PerlinNoise((x + item.噪声偏移.x) * fenquency, (y + item.噪声偏移.y) * fenquency) * 振幅;
                fenquency *= item.粗糙度;
                振幅 *= item.保留振幅;//小于一
            }
        }
        return y1;
    }

    public static Dictionary<Vector2Int,float> 柏林噪声叠加(List<Vector2Int> 坐标, List<噪声参数> 参数)
    {
        Dictionary<Vector2Int, float> 返回 = new Dictionary<Vector2Int, float>();
        float maxHeight = float.MinValue;
        float minHeight = float.MaxValue;


        foreach (var pos in 坐标)
        {
            float y1 = 0;
            foreach (var item in 参数)
            {
                float fenquency = item.噪声缩放;
                float 振幅 = item.高度;
                for (int i = 0; i < item.叠加次数; i++)
                {
                    y1 += Mathf.PerlinNoise((pos.x + item.噪声偏移.x) * fenquency, (pos.y + item.噪声偏移.y) * fenquency) * 振幅;
                    fenquency *= item.粗糙度;
                    振幅 *= item.保留振幅;//小于一
                }
            }
            if (y1 > maxHeight)
                maxHeight = y1;
            else if (y1 < minHeight)
                minHeight = y1;
            返回.Add(pos, y1);
        }

        foreach (var item in 返回)
        {
            返回[item.Key] = Mathf.InverseLerp(minHeight, maxHeight, 返回[item.Key]);
        }
       
        return 返回;
    }
}
